package com.cg.controller;

import com.cg.dto.*;
import com.cg.entity.User;
import com.cg.service.SalePlanService;
import com.cg.vo.QuerySalePlanVo;
import com.cg.vo.SalePlanVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SalePlan)表控制层
 *
 * @author chengong
 * @since 2023-12-24 10:27:37
 */
@RestController
@RequestMapping("/salePlan")
public class SalePlanController {
    /**
     * 服务对象
     */
    @Resource
    private SalePlanService salePlanService;

    /**
     * 销售计划管理列表
     */
    @PostMapping("/list")
    public Result<Map<String,Object>> list(@RequestBody SalePlanPageDto salePlanPageDto){
        if(salePlanPageDto.getSalePlanMonth() != null && !salePlanPageDto.getSalePlanMonth().equals("")){
            salePlanPageDto.setSalePlanMonth(salePlanPageDto.getSalePlanMonth()+"-01");
        }
        PageInfo<SalePlanVo> pageInfo = salePlanService.list(salePlanPageDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        return ResultUtils.returnDataSuccess(map);
    }


    /**
     * 创建销售计划
     */
    @PostMapping("/createSalePlan")
    public Result createSalePlan(@RequestBody CreateSalePlanDto createSalePlanDto){
        Integer count = salePlanService.createSalePlan(createSalePlanDto);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_CREATE_SALE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 查询销售计划详细信息
     */
    @GetMapping("/querySalePlan")
    public Result<QuerySalePlanVo> querySalePlan(@RequestParam("salePlanId") Integer salePlanId){
        QuerySalePlanVo querySalePlanVo = salePlanService.querySalePlan(salePlanId);
        if(querySalePlanVo == null){
            return ResultUtils.returnResult(ResultEnum.ERROR_SALE_NULL);
        }
        return ResultUtils.returnDataSuccess(querySalePlanVo);
    }


    /**
     * 加载所有的待处理人列表
     */
    @GetMapping("/pendingList")
    public Result<List<User>> pendingList(@RequestParam("userId") Integer userId){
        List<User> pendingList = salePlanService.pendingList(userId);
        if(pendingList == null){
            return ResultUtils.returnResult(ResultEnum.ERROR_SALE_ROLE);
        }
        return ResultUtils.returnDataSuccess(pendingList);
    }

    /**
     * 提交销售计划
     */
    @PostMapping("/submitSalePlan")
    public Result submitSalePlan(@RequestBody CreateSalePlanDto createSalePlanDto){
        Integer count = salePlanService.submitSalePlan(createSalePlanDto);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_SALE_STATE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 打回销售计划，通过，作废操作
     */
    @PostMapping("/passSalePlan")
    public Result passSalePlan(@RequestBody CreateSalePlanDto createSalePlanDto){
        Integer count = salePlanService.passSalePlan(createSalePlanDto);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_SALE_STATE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 删除销售计划，修改销售计划
     */
    @PostMapping("/removeSalePlan")
    public Result removeSalePlan(@RequestBody CreateSalePlanDto createSalePlanDto){
        Integer count = salePlanService.removeSalePlan(createSalePlanDto);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_SALE_STATE);
        }
        return ResultUtils.returnSuccess();
    }


}

